#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/9/17 16:06
# @Author  : youfeng
from config.mongo_conf import AppDataConfig
from common.mongo import MongDb


class MongoHandler(object):
    __GSXT_TABLE_NAME = 'enterprise_data_gov'
    __WENSHU_TABLE_NAME = 'judgement_wenshu'
    __ZHIXING_TABLE_NAME = 'zhixing_info'
    __SHIXIN_TABLE_NAME = 'shixin_info'
    __NEWS_TABLE_NAME = 'baidu_news'

    def __init__(self, log):
        self.log = log
        self.__app_data_db = MongDb(AppDataConfig.HOST, AppDataConfig.PORT, AppDataConfig.DB,
                                    AppDataConfig.USERNAME,
                                    AppDataConfig.PASSWORD, log=log)

    def get_gsxt_data(self, company):
        try:
            return self.__app_data_db.find_one(self.__GSXT_TABLE_NAME, {"company": company}, {"_id": False,
                                                                                              "_utime": False,
                                                                                              "_in_time": False,
                                                                                              "_src": False,
                                                                                              "_record_id": False})
        except Exception as e:
            self.log.error("查询工商数据失败:")
            self.log.exception(e)
        return None

    # 获得文书总数
    def get_wenshu_total(self, company):

        try:
            return self.__app_data_db.select_count({"generalized_litigant_list": company})
        except Exception as e:
            self.log.error("查询裁判文书总数目失败:")
            self.log.exception(e)
        return None

    # 获得文书摘要信息
    def get_wenshu_summary(self, company):
        result_list = []
        try:
            for item in self.__app_data_db.traverse_batch_field(self.__WENSHU_TABLE_NAME,
                                                                {"generalized_litigant_list": company},
                                                                {"_record_id": False,
                                                                 "_utime": False,
                                                                 "_in_time": False,
                                                                 "_src": False,
                                                                 "_id": False,
                                                                 "judge_content": False}):
                result_list.append(item)
            return result_list
        except Exception as e:
            self.log.error("查询裁判文书失败:")
            self.log.exception(e)
        return None

    def get_zhixing_data(self, company):
        result_list = []
        try:
            for item in self.__app_data_db.traverse_batch_field(self.__ZHIXING_TABLE_NAME,
                                                                {"i_name": company},
                                                                {"_record_id": False,
                                                                 "_utime": False,
                                                                 "_in_time": False,
                                                                 "_src": False,
                                                                 "_id": False}):
                result_list.append(item)
            return result_list
        except Exception as e:
            self.log.error("查询执行信息失败:")
            self.log.exception(e)
        return None

    def get_shixin_data(self, company):
        result_list = []
        try:
            for item in self.__app_data_db.traverse_batch_field(self.__SHIXIN_TABLE_NAME,
                                                                {"i_name": company},
                                                                {"_record_id": False,
                                                                 "_utime": False,
                                                                 "_in_time": False,
                                                                 "_src": False,
                                                                 "_id": False}):
                result_list.append(item)
            return result_list
        except Exception as e:
            self.log.error("查询失信信息失败:")
            self.log.exception(e)
        return None

    def get_news_data(self, company):
        result_list = []
        try:
            for item in self.__app_data_db.traverse_batch_field(self.__NEWS_TABLE_NAME,
                                                                {"keyword": company},
                                                                {"_record_id": False,
                                                                 "_utime": False,
                                                                 "_in_time": False,
                                                                 "_src": False,
                                                                 "_id": False}):
                result_list.append(item)
            return result_list
        except Exception as e:
            self.log.error("查询新闻失败:")
            self.log.exception(e)
        return None
